/**
 * 
 */
package cn.antia.cep.common.pool;

import java.util.*;

import cn.antia.cep.common.*;
import cn.antia.cwin.ConfigException;
import cn.antia.cwin.Cele;
import cn.antia.cwin.grid.SingleGrid;
import cn.antia.cwin.window.MultiWindow;
import cn.antia.cwin.window.SingleWindow;
import cn.antia.cep.db.hibernate.bean.Resource;

/**
 * @author sunkeei
 * @date Nov 20, 2007
 * @time 8:51:56 PM
 * @version 0.1
 */
public class AntiaPoolHelper {
	private AntiaPool antiaPool;
	public AntiaPool getAntiaPool() {
		return antiaPool;
	}
	public void setAntiaPool(AntiaPool antiaPool) {
		this.antiaPool = antiaPool;
	}
	
	/**
	 * 取通讯对象 (根据表名与字段名取通讯对象)
	 * @param table 表名
	 * @param field 字段名
	 * @return 通讯对象
     * @throws cn.antia.cep.common.AntiaException a*/
	public Cele getElementComsat(String table,String field)throws AntiaException{
		List elements = antiaPool.getElementComsats(table);
        for (Iterator it = elements.iterator(); it.hasNext();) {
            Object element = it.next();
            Cele comsat = (Cele) element;
            if (comsat.getField().getFieldName().equalsIgnoreCase(field)) {
                try {
                    return (Cele) comsat.clone();
                } catch (Exception e) {
                    AntiaLog.error("取数据对象时出现错误。CLONE错误。");
                }
            }
        }
        AntiaLog.debug("取表" + table + " 字段 " + field + " 的通讯对象时出错。如果是附加字段表示并没有出错。");
		return null;
	}
	
	/**
	 * 取得通信对象集合
	 * @param table 表名
	 * @return 通信对象列表
	 * @throws cn.antia.cep.common.AntiaException a*/
	public List getComsats(String table)throws AntiaException{
		List list = antiaPool.getElementComsats(table);
		List relist = new ArrayList();
		Cele ec;
		if(list != null && list.size() > 0){
			try{
				for(int i = 0;i<list.size();i++){
					ec = (Cele)list.get(i);
					relist.add(ec.clone());
				}
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}
		}
		return relist;
	}
	
	/**
	 * 增加资源的仿问量
	 * */
	public void addResourceClicks(int id)throws AntiaException{
		Resource resource = getResource(id);
		if(resource == null){
			AntiaLog.error("增加资源仿问量时没有找到指定的资源！可能已被删除！资源ID：" + id);
			return;
		}
		AntiaLog.debug("资源　" + id + " 增加了一个点击。");
		if(resource.getClickTimes() == null){
			resource.setClickTimes(new Integer(1));
			resource.isModify = true;
		}else{
			resource.setClickTimes(new Integer(resource.getClickTimes().intValue() + 1));
			resource.isModify = true;
		}
	}
	
	/**
	 * 根据特定的资源类型返回特定类型所对应的资源ID
	 * 返回为－1表示没有找到这个特定类型所对应的资源
	 * */
	public Resource getResourceBySpceType(int specType)throws AntiaException{
		List resources = antiaPool.getAllResources();
		Resource resource;
		for(int i = 0;i<resources.size();i++){
			resource = (Resource)resources.get(i);
			if(resource.getSpecType().intValue() == specType){
				return resource;
			}
		}
		return null;
	}
	
	public List getAllResources(){
		return antiaPool.getAllResources();
	}
	public void reObtainResources(){
		antiaPool.reObtainResources();
	}

	/**
	 * 取得指定父ID的RESOURCE（资源）列表
	 */
	public List getResources(int parentId) throws AntiaException {
		List relist = new ArrayList();
		List resources = antiaPool.getAllResources();
		Resource resource;
		for (int i = 0; i < resources.size(); i++) {
			resource = (Resource) resources.get(i);
			if (resource.getParentId().intValue() == parentId) {
				relist.add(resource);
			}
		}
		return relist;
	}

	/**
	 * 取得指定资源类型的所有资源
	 */
	public List getResourcesByResourceType(int resourceType)
			throws AntiaException {
		List relist = new ArrayList();
		List resources = antiaPool.getAllResources();
		Resource resource;
		for (int i = 0; i < resources.size(); i++) {
			resource = (Resource) resources.get(i);
			if (resource.getResourceType().intValue() == resourceType) {
				relist.add(resource);
			}
		}
		return relist;
	}
	
	/**
	 * 取得指定ID的RESOURCE（资源）
	 */
	public Resource getResource(int id) throws AntiaException {
		List resources = antiaPool.getAllResources();
		Resource resource;
		for (int i = 0; i < resources.size(); i++) {
			resource = (Resource) resources.get(i);
			if (resource.getId().intValue() == id) {
				return resource;
			}
		}
		return null;
	}
	/**
	 * 取单表表格配置
	 * @param gridId
	 * @return
	 */
	public SingleGrid getSingleGrid(String gridId){
		return antiaPool.getSingleGrid(gridId);
	}
	/**
	 * 取得单个WINDOW
	 * @param winId
	 * @return
	 */
	public SingleWindow getSingleWindow(String winId){
		SingleWindow window = antiaPool.getSingleWindow(winId);
		if(window == null){
			throw new ConfigException("012","window id:" + winId);
		}
		try{
			return (SingleWindow)window.clone();
		}catch(Exception e){
			throw new AntiaException("002","Clone SingleWindow error: Not support.");
		}
	}
	/**
	 * 取得单个WINDOW (不克隆对象)
	 * @param winId
	 * @return
	 */
	public SingleWindow getSingleWindowNoClone(String winId){
		return antiaPool.getSingleWindow(winId);
	}
	/**
	 * 取单个WINDOW的某个FIELD
	 * @param winId
	 * @param field
	 * @return
	 */
	public Cele getSingleWindowField(String winId,String field){
		SingleWindow window = antiaPool.getSingleWindow(winId);
		if(window == null){
			throw new ConfigException("012","window id:" + winId);
		}
		List list = window.getFields();
		Cele ec;
		for(int i = 0;i<list.size();i++){
			ec = (Cele)list.get(i);
			if(ec.getField().getFieldName().equals(field)){
				try{
					return (Cele)ec.clone();
				}catch(Exception e){
					throw new AntiaException("002","Clone Cele error: Not support.");
				}
			}
		}
		return null;
	}
	/**
	 * 取得多窗口WINDOW (不克隆对象)
	 * @param id
	 * @return
	 */
	public MultiWindow getMultiWindow(String id){
		MultiWindow window = antiaPool.getMultiWindow(id);
		if(window == null){
			throw new ConfigException("012","window id:" + id);
		}
		try{
			return (MultiWindow)window.clone();
		}catch(Exception e){
			throw new AntiaException("002","Clone SingleWindow error: Not support.");
		}
	}
	/**
	 * 取得单个WINDOW (不克隆对象)
	 * @param winId
	 * @return
	 */
	public MultiWindow getMultiWindowNoClone(String winId){
		return antiaPool.getMultiWindow(winId);
	}
}
